package com.ztjava.weixin.service;

import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.ztjava.core.base.BaseService;
import com.ztjava.core.client.Client;
import com.ztjava.weixin.model.menu.MenuGroup;
import com.ztjava.weixin.model.menu.MenuItem;

public class MenuService extends BaseService{
	/*************
	 *Group分页
	**************/
	public Page<MenuGroup> getGroupPage(Client client,Map<String,String[]> paramMap){
		String[][] param = new String[][]{	
				new String[]{"name",like,"name"}
		,new String[]{"sex",eq,"sex"}			};
		Page<MenuGroup> pd = MenuGroup.dao.paginate(
				getParaToInt(paramMap, client.getPageNumber(), 1), 
				getParaToInt(paramMap,client.getPageSize(),20),
				"select *","from "+MenuGroup.tablename+" where 1=1 " 				
				+ whereSql(paramMap,param)
				+ orderby(client, paramMap),whereParam(paramMap, param));
		changeBasedata(pd.getList()			
		, new String[]{"sex","WEIXIN_MENU_GROUP_SEX"});
		return pd;
	}
	/*************
	 *Item列表
	**************/
	public List<MenuItem> getItemList(Integer group_id){
		return MenuItem.dao.find("select id,parent_id as pId,name,num from "+MenuItem.tablename+" where group_id="+group_id+" order by sort asc,id asc");
	}
	/*************
	 *Item检查是否存在子内容
	**************/
	public boolean checkChildItem(Integer parent_id){
		if(MenuItem.dao.findFirst("select id from "+MenuItem.tablename+" where parent_id=?",parent_id)!=null){
			return false;
		}
		return true;
	}
	public Integer getChildItemAmount(Integer group_id,Integer parent_id){
		return Db.queryInt("select count(id) from "+MenuItem.tablename+" where parent_id=? and group_id=?",parent_id,group_id);
	}
	public List<MenuItem> getGroupItemList(Integer group_id){
		return MenuItem.dao.find("select * from "+MenuItem.tablename+" where group_id=? order by sort asc,id asc",group_id);
	}
	
	public void updateGroupMenuid(){
		Db.update("update "+MenuGroup.tablename+" set menuid=null");
	}
}
